// pages/gsxl/gsxl.js
const app = getApp();
const sendMessage = require('../../../pages/home/home')
const util = require('../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    second:'',
    times:0,
    trainType:'',
    speed:'',
    url:'',
    img:["/images/0.6T.png","/images/0.6R.png","/images/0.6D.png","/images/0.6L.png"],
    list:[],
    selected:"",
    visualSize:'',
    eyeType:'',
    danciList:[],
    nums:1,
    setInter:'',
    page:1,
    ispaly:'',
    inter:'',
    grade:0,
    equipmentNumber:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    var trainType = options.trainType;
    var eyeType = options.eyeType;
    var time = options.time;
    if (trainType == '视标模式'){
      var list = [];
      for(var i=0;i<1;i++){
        let random = Math.floor(Math.random() * 4);
        var selected = false;
        if(i == 0){
          selected = true;
        }
        list.push({"random":random,"selected":selected,"num":i});
      }
      that.setData({
        list:list,
        selected:0,
        trainType:trainType,
        eyeType:eyeType,
        second:time *60
      });
    } else if (trainType == '单词模式'){
      that.setData({
        trainType:trainType,
        eyeType:eyeType,
        second:time *60
      })
      util.danciList(that);
      that.startInter();
      that.startInterval();
    } else if (trainType == '色图模式'){
      that.setData({
        trainType:trainType,
        eyeType:eyeType,
        second:time *60
      })
      that.startInterval();
    }
      that.startCountdown();
  },
  startInter(){
    var that = this;
    that.data.setInter = setInterval(
      function () {
        that.setData({
          nums:that.data.nums+1
        })
        if (that.data.danciList[0].num ==1){
          that.setData({
            nums:2
          })
        }
        util.danciList(that);
      }
, 5000)
  },
  /**点击 */
dianji(e){
  var that = this;
  var type = e.target.dataset.type;
  var selected = that.data.selected;
  var list = that.data.list;
  var corrects = that.data.correct;
    if(type == list[selected].random){
      list[selected].selected = false;
      if(selected+1 < list.length){
        console.log("1213");
        selected += 1;
        list[selected].selected = true;
      }else{
        list = [];
        console.log("4546");
        for(var i=0;i<1;i++){
          let random = Math.floor(Math.random() * 4);
          var selected = false;
          // if (this.data.eyeType =='右眼'){
          //   if(i == 2){
          //     selected = true;
          //   }
          // } else {
          //   if(i == 0){
          //     selected = true;
          //   }
          // }
          if(i == 0){
            selected = true;
          }
          list.push({"random":random,"selected":selected,"num":i});
        }
        selected = 0;
        list[0].selected = true;
      }
      console.log("正确 "+"selected："+selected);
      sendMessage.BLEDataSend("aa55a55a210100cc66c66c");
      corrects +=1;
      that.setData({
        list:list,
        selected:selected,
        correct:corrects
      });
      that.end();
      that.start();
    }else{
      if(type == 4){
        console.log("看不清 "+"selected："+selected);
        list[selected].selected = false;
        if(selected+1 < list.length){
          selected += 1;
          list[selected].selected = true;
        }else{
          selected = 0;
          list[0].selected = true;
        }
        that.setData({
          list:list,
          selected:selected
        });
        that.end();
        that.start();
      }else{
        var errors = that.data.error;
        errors +=1;
        var grade=that.data.grade;
        grade = grade+1;
        that.setData({
          error:errors,
          grade:grade
        })
        wx.showToast({
          title: "错误",
          icon: "none",
          mask: false, 
          duration:1000 
        });
      }
    }
 
},
start(){
  var that = this;
  var time =  that.data.time;
  // var timer = setInterval(()=>{
  //      time += 0.01;
  //      that.setData({
  //       time:time.toFixed(2),
  //      })
  // },10);
  // that.setData({
  //   timer:timer,
  // });
},
end(){
  var that = this;
  console.log("结束：");
  that.setData({
    time:0,
  });
 // clearInterval(that.data.timer);
},
toOne:function (e){
  var viewText = e.currentTarget.dataset.text;
  this.setData({
    ispaly:viewText
  })
  setTimeout(() => {
    this.cardPage();
  }, 200);
  //this.cardPage();
},
toTwo:function (e){
  var viewText = e.currentTarget.dataset.text;
  this.setData({
    ispaly:viewText
  })
  setTimeout(() => {
    this.cardPage();
  }, 200);
},
toThree:function (e){
  var viewText = e.currentTarget.dataset.text;
  this.setData({
    ispaly:viewText
  })
  setTimeout(() => {
    this.cardPage();
  }, 200);
},
toFour:function (e){
  var viewText = e.currentTarget.dataset.text;
  this.setData({
    ispaly:viewText
  })
  setTimeout(() => {
    this.cardPage();
  }, 200);
},
cardPage(){
  var page = this.data.page;
  page = page +1;
  this.setData({
    page:page,
  })
  if (page == 21){
    this.setData({
      page:1,
    })
  }
},
  startCountdown() {
    var that = this;
    var second = that.data.second
    var traintype=that.data.trainType;
    var grade = that.data.grade;
    var equipmentNumber = app.nowEquipmentId;
    var score = 0;
    if (second == 0) {
      clearInterval(that.data.timer);
      clearInterval(that.data.inter);
      if (traintype == '视标模式'){
        if (grade <=3){
          score = 5;
        } else if (grade <=6 && grade >3){
          score = 4;
        } else if (grade <=9 && grade >6){
          score = 3;
        } else {
          score = 2;
        }
      } else {
        score = 5;
      }
      wx.navigateTo({
        url: '../../../pages/fraction/fraction?fraction='+score,
      })
    }
    that.data.timer = setTimeout(function () {
      that.setData({
        second: second - 1
      });
      that.startCountdown();
    } , 1000)
    var times = that.data.times;
    times++;
    that.setData({
      times:times,
      equipmentNumber:equipmentNumber
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log("f返回1")
    var openId= wx.getStorageSync('openid');
    clearInterval(this.data.timer);
    clearInterval(this.data.inter);
    this.setData({
      trainName:"光栅训练",
      openId:openId,
      times:180
    })
    util.saveRecord(this);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    console.log("f返回2")
    var second = this.data.second;
    if (second ==0){
      return;
    }
    var openId= wx.getStorageSync('openid');
    clearInterval(this.data.timer);
    clearInterval(this.data.inter);
    this.setData({
      trainName:"光栅训练",
      openId:openId
    })
    util.saveRecord(this);
  },
  startInterval: function(){
    var that = this;
    that.data.inter= setInterval(function () {
      sendMessage.BLEDataSend("aa55a55a210100cc66c66c");
      console.log("定时任务开启");
    }, 10000)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})